using System.Text.RegularExpressions;
using Newtonsoft.Json;
using Weather.Model;

namespace Weather;

class Program
{
    private static Config _config;
    static void Main(string[] args)
    {
        InitConfig();
        string city = _config.DefaultCity;
        if(args.Length > 1)
        {
            Utils.WriteLine("参数过多，请仅填写位置（省略“省、市、县、区等”）。 如： 陕西西安雁塔", ConsoleColor.Red);
            return;
        }
        if(args.Length == 1)
        {
            city = args[0];
        }

        string htmlData = TryGetWeatherHtmlData(city, out string errorMsg);
        if(!string.IsNullOrEmpty(errorMsg))
        {
            Utils.WriteLine(errorMsg, ConsoleColor.Red);
            return; 
        }
        WeatherData? weatherData = GetWeatherData(htmlData, out errorMsg);
        if(!string.IsNullOrEmpty(errorMsg))
        {
            Utils.WriteLine(errorMsg, ConsoleColor.Red);
            return;
        }
        if(weatherData == null)
        {
            Utils.WriteLine("未获取到天气数据", ConsoleColor.Red);
            return;
        }
        PrintWeatherData(weatherData);
    }
    private static void InitConfig()
    {
        try
        {
            if(_config == null)
                _config = new Config(){DefaultCity = "陕西西安"};
            Config? config = null;
            string appPath = AppDomain.CurrentDomain.BaseDirectory;
            string configPath = Path.Join(appPath, "config.json");
            if(File.Exists(configPath))
            {
                string jsonStr = File.ReadAllText(configPath);
                config = JsonConvert.DeserializeObject<Config>(jsonStr); 
            }
            else
            {
                string jsonStr = JsonConvert.SerializeObject(_config);
                File.WriteAllText(configPath, jsonStr); 
            }

            if(config != null && !string.IsNullOrEmpty(config.DefaultCity))
            {
                _config.DefaultCity = config.DefaultCity;
            }
        }
        catch(Exception ex)
        {
            Utils.WriteLine(ex.ToString(), ConsoleColor.Red);
        }
        
    }
    private static WeatherData? GetWeatherData(string htmlData, out string errorMsg)
    {        
        errorMsg = string.Empty;
        WeatherData? weatherData = null;
        
        if(string.IsNullOrEmpty(htmlData))
        {
            errorMsg = "未获取到天气数据";
            return weatherData;
        }
        if(Regex.IsMatch(htmlData, @"暂未开通此城市查询"))
        {
            errorMsg = "暂未开通此城市查询";
            return weatherData; 
        }        
        try
        {
            // 使用正则表达式提取 window.modifyData 的值
            string pattern = @"window.modifyData\s*=\s*({.*?});";
            Match match = Regex.Match(htmlData, pattern, RegexOptions.Singleline);
            if (match.Success)
            {
                // 提取匹配到的 JSON 数据
                string tplDataJson = DecodeUnicode(match.Groups[1].Value);
                //File.WriteAllText("data,json", tplDataJson);
                weatherData = JsonConvert.DeserializeObject<WeatherData>(tplDataJson);
                if (weatherData == null)
                {
                    errorMsg = "解析天气数据失败";                    
                }
            }
            else
            {
                errorMsg = "未找到正确的天气数据";
            }
        }
        catch (Exception ex)
        {
            errorMsg = ex.Message;
            return weatherData;
        }
        return weatherData;
        
    }
    private static string TryGetWeatherHtmlData(string city, out string errorMsg)
    {
        string htmlData = string.Empty;
        errorMsg = string.Empty;
        try
        {
            CancellationToken token = new CancellationTokenSource(5000).Token;
            Task<string> task = Task<string>.Factory.StartNew((objArg) =>
            {
                if(objArg is string cityStr && !string.IsNullOrEmpty(cityStr))
                    return GetWeatherHtmlDataSync(city).Result;
                else
                    return "暂未开通此城市查询";
            }, city, token);
            htmlData = task.Result;
        }
        catch (Exception ex)
        {
            errorMsg = ex.Message;
            return htmlData;
        }
        return htmlData;
    }
    private static async Task<string> GetWeatherHtmlDataSync(string city)
    {
        string htmlData = string.Empty;
        using (HttpClient client = new HttpClient())
        {
            // 定义请求的 URL
            string url = $"https://weathernew.pae.baidu.com/weathernew/pc?query={city}天气&srcid=4982";

            // 发送 GET 请求并获取响应内容
            HttpResponseMessage response = await client.GetAsync(url);
            response.EnsureSuccessStatusCode();
            htmlData = await response.Content.ReadAsStringAsync();
        }
        return htmlData;
    }
    private static string DecodeUnicode(string input)
    {
        return Regex.Replace(input, @"\\u([0-9A-Fa-f]{4})", m =>
        {
            short code = Convert.ToInt16(m.Groups[1].Value, 16);
            return ((char)code).ToString();
        });
    }

    private static void PrintWeatherData(WeatherData weatherData)
    {
        Console.OutputEncoding = System.Text.Encoding.UTF8;
        int maxWidth = 120;//Console.WindowWidth;
        PrintWeatherBaseInfo(weatherData, maxWidth);
        Utils.WriteLine("");
        PrintWeatherCurentInfo(weatherData, maxWidth);
        Utils.WriteLine("");
        PrintWeatherNext24HourInfo(weatherData, maxWidth);
        Utils.WriteLine("");
        PrintWeatherNext15DayInfo(weatherData, maxWidth);
    }

    private static void PrintWeatherBaseInfo(WeatherData weatherData, int maxWidth)
    {
        string position = "📍"+weatherData.Position.City;
        string time = "📅"+weatherData.Base.DateShort+" "+weatherData.Base.Weekday + " " + weatherData.Base.Lunar;
        Utils.Write(position, ConsoleColor.Magenta, ConsoleColor.Yellow);
        Utils.Write(" | ", ConsoleColor.Red);        
        Utils.Write(time, ConsoleColor.Blue); 
        Utils.Write(" | ",ConsoleColor.Red);

        string weather = string.Empty;
        if(weatherData.Weather.WeatherDay == weatherData.Weather.WeatherNight)
            weather = weatherData.Weather.WeatherDay;
        else
            weather = weatherData.Weather.WeatherNight+"转"+weatherData.Weather.WeatherDay;
        string temperature = string.Empty;
        if(weatherData.Weather.TemperatureDay == weatherData.Weather.TemperatureNight)
            temperature = weatherData.Weather.TemperatureDay+"℃";
        else
            temperature = weatherData.Weather.TemperatureNight+"~"+weatherData.Weather.TemperatureDay+"℃";
        string wind = string.Empty;
        if(weatherData.Weather.WindDirectionDay == weatherData.Weather.WindDirectionDay
            && weatherData.Weather.WindPowerDay == weatherData.Weather.WindPowerNight)
            wind = weatherData.Weather.WindDirectionDay + weatherData.Weather.WindPowerDay;
        else
            wind = weatherData.Weather.WindDirectionNight + weatherData.Weather.WindPowerNight
                    + "转" +weatherData.Weather.WindDirectionDay + weatherData.Weather.WindPowerDay;
                
        Utils.Write(weather, ConsoleColor.Yellow);
        Utils.Write(" ");
        Utils.Write("🌡️"+temperature, ConsoleColor.Yellow);
        Utils.Write(" ");
        Utils.Write("🎐"+wind, ConsoleColor.Green);
        Utils.WriteLine("");
    }
    private static void PrintWeatherCurentInfo(WeatherData weatherData, int maxWidth)
    {
        var currentTemperature = weatherData.Weather.Temperature;
        if(!int.TryParse(currentTemperature, out int temperature))
            return;
        NumberPixArryHelper helper = new NumberPixArryHelper(NumberPixArryHelper.Size.Small);
        List<string[,]> numberPixArrys = new List<string[,]>();
        bool isPositive = temperature >= 0;
        string currentTemperatureStr = currentTemperature;
        if(!isPositive)
        {
            currentTemperatureStr = currentTemperatureStr.Substring(1);
            int[,] negPixArr = NumberPixArryHelper.CreatEmptyArray(helper.RowCount,3);
            negPixArr[helper.Width-1, 0] = 1;
            negPixArr[helper.Width-1, 1] = 1;
            numberPixArrys.Add(NumberPixArryHelper.ConvertIntArryToStrArry(negPixArr));
        }
        
        int rowCount = helper.RowCount;
        numberPixArrys.AddRange(GetPisxArryList(helper, currentTemperatureStr));
        int[,] split = NumberPixArryHelper.CreatEmptyArray(helper.RowCount, 1);
        string[,] splitStrs = NumberPixArryHelper.ConvertIntArryToStrArry(split);
        splitStrs[0,0] = " ◯";   
        numberPixArrys.Add(splitStrs);             
        int tempPisArrWidth = numberPixArrys.Sum(arr=>arr.GetLength(1));
        
        
        ConsoleColor color = ConsoleColor.Green;
        if(temperature > 28)
            color = ConsoleColor.Red;
        else if(temperature < 18)
            color = ConsoleColor.Blue;
        string[] lines = new string[rowCount];
        for (int i = 0; i < rowCount; i++)
            lines[i] = string.Empty;
        foreach(var arr in numberPixArrys)
        {
            for (int r = 0; r < rowCount; r++)
            {
                for(int c = 0; c < arr.GetLength(1); c++) 
                {
                        lines[r] += arr[r,c]; 
                }
            }
        }
        for (int i = 0; i < rowCount; i++)
        {
            Utils.Write(lines[i], color);
            Utils.Write("  ");
            WriteCurentExtInfo(weatherData, i);
            Utils.WriteLine("");
        }

    }
    private static List<string[,]> GetPisxArryList(NumberPixArryHelper helper, string intStr)
    {
        List<string[,]> pixArryList = new List<string[,]>();
        for (int i = 0; i < intStr.Length; i++)
        {
            char numChar = intStr[i];
            if(numChar < '0' || numChar > '9')
                continue;
            int[,] pix = helper.GetNumberPixArry(numChar);
            string[,] arr = NumberPixArryHelper.ConvertIntArryToStrArry(pix);        
            pixArryList.Add(arr);
            if(i < intStr.Length -1)
            {
                int[,] split = NumberPixArryHelper.CreatEmptyArray(helper.RowCount, 1);
                string[,] splitStrs = NumberPixArryHelper.ConvertIntArryToStrArry(split);
                pixArryList.Add(splitStrs);
            }
        }
        return pixArryList;
    }
    private static void WriteCurentExtInfo(WeatherData weatherData, int row)
    {
        switch (row)
        {
            case 1:
                WriteAir(weatherData);
                break;
            case 2:
                WriteWeather(weatherData);
                break;
            case 3:
                WriteSunTime(weatherData);
                break;
        }
    }

    private static void WriteAir(WeatherData weatherData)
    {      
        if(double.TryParse(weatherData.Weather.Humidity, out double humidity))
        {
            ConsoleColor color = ConsoleColor.DarkGreen;
            if(humidity > 80)
                color = ConsoleColor.DarkBlue;
            else if(humidity < 30)
                color = ConsoleColor.DarkRed;
            Utils.Write($"湿度：{humidity}%", ConsoleColor.Black, color);
        }  
        Utils.Write("  ");
        if(double.TryParse(weatherData.PsPm25.PsPm25, out double pm25))
        {
            ConsoleColor color = ConsoleColor.DarkGreen;
            if(pm25 > 100)
                color = ConsoleColor.DarkYellow;
            else if(pm25 > 200)
                color = ConsoleColor.Red;
            Utils.Write($"{pm25} {weatherData.PsPm25.Level}", ConsoleColor.Black, color);
        }
    }
    private static void WriteWeather(WeatherData weatherData)
    {
        string icon = string.Empty;
        foreach(var item in weatherData.Weather.Weather)
        {
            if(_weatherIcons.ContainsKey(item))
            {
                icon = _weatherIcons[item];
                break;
            } 
        }
        Utils.Write(icon + weatherData.Weather.Weather, ConsoleColor.Blue);        
        Utils.Write(" ");
        string windDirection = weatherData.Weather.WindDirection;
        string windPower = weatherData.Weather.WindPower;
        string windIconKey = windDirection.Replace("风","");
        if(_windIcons.ContainsKey(windIconKey))
            windDirection = _windIcons[windIconKey] + windDirection;
        Utils.Write(windDirection + windPower, ConsoleColor.Green);
    }
    private static void WriteSunTime(WeatherData weatherData)
    {
        string icon = string.Empty;
        foreach(var item in weatherData.Weather.Weather)
        {
            if(_weatherIcons.ContainsKey(item))
            {
                icon = _weatherIcons[item];
                break;
            } 
        }
        Utils.Write("🌅 " + weatherData.Feature.SunriseTime, ConsoleColor.Yellow);
        Utils.Write(" ");
        Utils.Write("🌇 " + weatherData.Feature.SunsetTime, ConsoleColor.Blue);        
        
    }

    private static void PrintWeatherNext24HourInfo(WeatherData weatherData, int maxWidth)
    {
        List<string> hours = new List<string>();
        List<string> temperatures = new List<string>();
        List<string> rains = new List<string>();
        List<string> airs = new List<string>();
        List<string> windDirections = new List<string>();
        foreach(var time in weatherData.TimeNodeArr) 
        {
           if(time == "现在")
                hours.Add("NOW");
            else
                hours.Add(time); 
        }
        foreach(var item in weatherData.TemperaturePointArr)
        {
            if(item is object[] && item.Length > 2 && item[2] is string t)
                temperatures.Add(t);
            else
                temperatures.Add("");
        }
        foreach(var item in weatherData.PrecipitationPointArr)
        {
            if(item is object[] && item.Length > 2 && item[2] is string t)
                rains.Add(t);
            else
                rains.Add("");
        }
        foreach(var item in weatherData.PmPointArr)
        {
            if(item is object[] && item.Length > 2 && item[2] is string t)
                airs.Add(t);     
        }
        foreach(var item in weatherData.WindPointArr)
        {
            if(item is object[] && item.Length > 3 && item[2] is string l && item[3] is string d)
            {
                string windIconKey = d.Replace("风","");
                if(_windIcons.ContainsKey(windIconKey))
                    windDirections.Add(_windIcons[windIconKey] +l);
                else
                    windDirections.Add("?"+l);
            }
            else
                windDirections.Add("");
        }
        if(hours.Count < 24 || temperatures.Count < 24 || rains.Count < 24 || airs.Count < 24 || windDirections.Count < 24)
            return;
        Utils.Write("未来24小时天气", ConsoleColor.Black, ConsoleColor.Green);Utils.WriteLine("");
        Utils.Write("时间|", ConsoleColor.Black,ConsoleColor.Blue); 
        for(int i = 0; i < 12; i++){Utils.Write(FormatText(hours[i], 6, ' ') + "|", ConsoleColor.Black, ConsoleColor.Blue);}
        Utils.WriteLine("");

        Utils.Write("温度", ConsoleColor.Black,ConsoleColor.Green); Utils.Write("|", ConsoleColor.Blue);
        for(int i = 0; i < 12; i++){Utils.Write(FormatText(temperatures[i], 6,' '), ConsoleColor.Green); Utils.Write("|", ConsoleColor.Blue);}
        Utils.WriteLine("");

        Utils.Write("降水", ConsoleColor.Black,ConsoleColor.Magenta); Utils.Write("|", ConsoleColor.Blue);
        for(int i = 0; i < 12; i++){Utils.Write(FormatText(rains[i], 6,' '), ConsoleColor.Magenta); Utils.Write("|", ConsoleColor.Blue);}
        Utils.WriteLine("");

        Utils.Write("空气", ConsoleColor.Black,ConsoleColor.DarkYellow); Utils.Write("|", ConsoleColor.Blue);
        for(int i = 0; i < 12; i++){Utils.Write(FormatText(airs[i], 6,' '), ConsoleColor.DarkYellow); Utils.Write("|", ConsoleColor.Blue);}
        Utils.WriteLine("");

        Utils.Write("风向", ConsoleColor.Black,ConsoleColor.Cyan); Utils.Write("|", ConsoleColor.Blue);
        for(int i = 0; i < 12; i++){Utils.Write(FormatText(windDirections[i], 6,' '), ConsoleColor.Cyan); Utils.Write("|", ConsoleColor.Blue);}
        Utils.WriteLine("");
        
        Utils.Write("时间|", ConsoleColor.Black,ConsoleColor.Blue);
        for(int i = 12; i < 24; i++){Utils.Write(FormatText(hours[i], 6, ' ')+"|", ConsoleColor.Black, ConsoleColor.Blue);}
        Utils.WriteLine("");

        Utils.Write("温度", ConsoleColor.Black,ConsoleColor.Green); Utils.Write("|", ConsoleColor.Blue);
        for(int i = 12; i < 24; i++){Utils.Write(FormatText(temperatures[i], 6,' '), ConsoleColor.Green); Utils.Write("|", ConsoleColor.Blue);}
        Utils.WriteLine("");

        Utils.Write("降水", ConsoleColor.Black,ConsoleColor.Magenta); Utils.Write("|", ConsoleColor.Blue);
        for(int i = 12; i < 24; i++){Utils.Write(FormatText(rains[i], 6,' '), ConsoleColor.Magenta); Utils.Write("|", ConsoleColor.Blue);}
        Utils.WriteLine("");

        Utils.Write("空气", ConsoleColor.Black,ConsoleColor.DarkYellow); Utils.Write("|", ConsoleColor.Blue);
        for(int i = 12; i < 24; i++){Utils.Write(FormatText(airs[i], 6,' '), ConsoleColor.DarkYellow); Utils.Write("|", ConsoleColor.Blue);}
        Utils.WriteLine("");

        Utils.Write("风向", ConsoleColor.Black,ConsoleColor.Cyan); Utils.Write("|", ConsoleColor.Blue);
        for(int i = 12; i < 24; i++){Utils.Write(FormatText(windDirections[i], 6,' '), ConsoleColor.Cyan); Utils.Write("|", ConsoleColor.Blue);}
        Utils.WriteLine("");
    }
    private static void PrintWeatherNext15DayInfo(WeatherData weatherData, int maxWidth)
    {
        List<string> weeks = new List<string>();
        List<string> date = new List<string>();
        List<string> weathers = new List<string>();
        List<string> dayTemp = new List<string>();
        List<string> nightTep = new List<string>();
        List<string> wind = new List<string>();
        List<string> airs = new List<string>();
        foreach(var item in weatherData.Weather15DayData) 
        {
            weeks.Add(item.FormatWeek);
            if(item.Date.Contains('-'))
            {
                var dateArr = item.Date.Split('-');
                date.Add(dateArr[1] + "-" + dateArr[2]);
            }
            else
                date.Add(item.Date);
            weathers.Add(item.WeatherText);
            string windIconKey = item.WeatherWind.WindDirectionDay.Replace("风","");
            if(_windIcons.ContainsKey(windIconKey))
                wind.Add(_windIcons[windIconKey] +item.WeatherWind.WindPowerDay);
            else
                wind.Add("?"+item.WeatherWind.WindPowerDay);
            airs.Add(item.WeatherPm25);           
        }
        foreach(var item in weatherData.TemperatureDayList)
        {
            dayTemp.Add(item.Temperature);
        }
        foreach(var item in weatherData.TemperatureNightList)
        {
            nightTep.Add(item.Temperature);
        }
        if(date.Count < 15 || weathers.Count < 15 || dayTemp.Count < 15 || nightTep.Count < 15 || wind.Count < 15 || airs.Count < 15)
            return;
        
        Utils.Write("15天 天气", ConsoleColor.Black, ConsoleColor.Green);
        Utils.Write($" 昨天：{weathers[0]} {nightTep[0]}~{dayTemp[0]} {wind[0]} {airs[0]}", ConsoleColor.DarkGray);
        Utils.WriteLine("");

        Utils.Write(" 时  间 |", ConsoleColor.Black,ConsoleColor.Blue);
        for(int i = 1; i < 8; i++){Utils.Write(FormatText(weeks[i], 10, ' ') + "|", ConsoleColor.Black, ConsoleColor.Blue);}
        Utils.WriteLine("");
        Utils.Write(" 日  期 |", ConsoleColor.Black,ConsoleColor.Blue);
        for(int i = 1; i < 8; i++){Utils.Write(FormatText(date[i], 10, ' ') + "|", ConsoleColor.Black, ConsoleColor.Blue);}
        Utils.WriteLine("");

        Utils.Write("天气状况", ConsoleColor.Black,ConsoleColor.Green); Utils.Write("|", ConsoleColor.Blue);
        for(int i = 1; i < 8; i++){Utils.Write(FormatText(weathers[i], 10,' '), ConsoleColor.Green); Utils.Write("|", ConsoleColor.Blue);}
        Utils.WriteLine("");

        Utils.Write("最高气温", ConsoleColor.Black,ConsoleColor.Magenta); Utils.Write("|", ConsoleColor.Blue);
        for(int i = 1; i < 8; i++){Utils.Write(FormatText(dayTemp[i], 10,' '), ConsoleColor.Magenta); Utils.Write("|", ConsoleColor.Blue);}
        Utils.WriteLine("");

        Utils.Write("最低气温", ConsoleColor.Black,ConsoleColor.DarkYellow); Utils.Write("|", ConsoleColor.Blue);
        for(int i = 1; i < 8; i++){Utils.Write(FormatText(nightTep[i], 10,' '), ConsoleColor.DarkYellow); Utils.Write("|", ConsoleColor.Blue);}
        Utils.WriteLine("");

        Utils.Write("风向风力", ConsoleColor.Black,ConsoleColor.Cyan); Utils.Write("|", ConsoleColor.Blue);
        for(int i = 1; i < 8; i++){Utils.Write(FormatText(wind[i], 10,' '), ConsoleColor.Cyan); Utils.Write("|", ConsoleColor.Blue);}
        Utils.WriteLine("");

        Utils.Write("空气污染", ConsoleColor.Black,ConsoleColor.DarkRed); Utils.Write("|", ConsoleColor.Blue);
        for(int i = 1; i < 8; i++){Utils.Write(FormatText(airs[i], 10,' '), ConsoleColor.DarkRed); Utils.Write("|", ConsoleColor.Blue);}
        Utils.WriteLine("");
        
        Utils.Write(" 时  间 |", ConsoleColor.Black,ConsoleColor.Blue);
        for(int i = 8; i < 15; i++){Utils.Write(FormatText(weeks[i], 10, ' ') + "|", ConsoleColor.Black, ConsoleColor.Blue);}
        Utils.WriteLine("");
        Utils.Write(" 日  期 |", ConsoleColor.Black,ConsoleColor.Blue);
        for(int i = 8; i < 15; i++){Utils.Write(FormatText(date[i], 10, ' ') + "|", ConsoleColor.Black, ConsoleColor.Blue);}
        Utils.WriteLine("");

        Utils.Write("天气状况", ConsoleColor.Black,ConsoleColor.Green); Utils.Write("|", ConsoleColor.Blue);
        for(int i = 8; i < 15; i++){Utils.Write(FormatText(weathers[i], 10,' '), ConsoleColor.Green); Utils.Write("|", ConsoleColor.Blue);}
        Utils.WriteLine("");

        Utils.Write("最高气温", ConsoleColor.Black,ConsoleColor.Magenta); Utils.Write("|", ConsoleColor.Blue);
        for(int i = 8; i < 15; i++){Utils.Write(FormatText(dayTemp[i], 10,' '), ConsoleColor.Magenta); Utils.Write("|", ConsoleColor.Blue);}
        Utils.WriteLine("");

        Utils.Write("最低气温", ConsoleColor.Black,ConsoleColor.DarkYellow); Utils.Write("|", ConsoleColor.Blue);
        for(int i = 8; i < 15; i++){Utils.Write(FormatText(nightTep[i], 10,' '), ConsoleColor.DarkYellow); Utils.Write("|", ConsoleColor.Blue);}
        Utils.WriteLine("");

        Utils.Write("风向风力", ConsoleColor.Black,ConsoleColor.Cyan); Utils.Write("|", ConsoleColor.Blue);
        for(int i = 8; i < 15; i++){Utils.Write(FormatText(wind[i], 10,' '), ConsoleColor.Cyan); Utils.Write("|", ConsoleColor.Blue);}
        Utils.WriteLine("");
    }
    
    private static string FormatText(string text, int maxLength, char fillChar = ' ')
    {
        string result = "";
        string head = "";
        string tail = "";
        int tailLength = 0;
        int actualLength = 0;
        int textLength = text.Length;
        for(int i = 0; i < maxLength; i++)
        {
            char c = fillChar;
            int charWidth = 1;
            if(i < textLength)
            {
                c = text[textLength - i - 1];                
                if(char.GetUnicodeCategory(c) == System.Globalization.UnicodeCategory.OtherLetter || (c >= 0xFF01 && c <= 0xFF5E)) 
                    charWidth = 2;
                if((actualLength + charWidth) < maxLength-3)
                {
                    tail = c + tail;
                    tailLength += charWidth;
                    actualLength += charWidth;
                }
                else if((actualLength + charWidth) <= maxLength)
                {
                    head = c + head;
                    actualLength += charWidth;
                }
            } 
        }
        int padding = maxLength - actualLength;
        if(padding >= 0)
        {
            result = head + tail;
            for(int i = 0; i < padding; i++)
            {
                if(i % 2 == 0)
                    result = fillChar + result;
                else
                    result = result + fillChar;
            }
        }
        else
        {
            result = string.Join("", Enumerable.Repeat('.', maxLength - tailLength)) + tail;
        }
        return result;
    }
    
    
    
    private static Dictionary<char, string> _weatherIcons = new Dictionary<char, string>()
    {
        {'雨',"🌧️"},
        {'晴',"☀️"},
        {'云',"☁️"},
        {'阴',"⛅"},
        {'雪',"🌨️"},
    };

    private static Dictionary<string, string> _windIcons = new Dictionary<string, string>()
    {
        {"东","➡️"},
        {"东南","↘️"},
        {"南","⬇️"},
        {"西南","↙️"},
        {"西","⬅️"},
        {"西北","↖️"},
        {"北","⬆️"},
        {"东北","↗️"},
    };
}